﻿// -----------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Globalization;

namespace System.ComponentModel.Composition
{
    [Serializable]
    public class CompositionIssue
    {
        public string Id { get; private set; }
        public CompositionIssueSeverity Severity { get; private set; }
        public string Description { get; private set; }
        public Exception Exception { get; private set; }
        public IDictionary<string, object> Details { get; private set; }

        public CompositionIssue(string id, CompositionIssueSeverity severity, string description, Exception exception)
        {
            this.Id = id;
            this.Severity = severity;
            this.Description = description;
            this.Exception = exception;
            this.Details = new Dictionary<string, object>();
        }

        public override string ToString()
        {
            List<string> details = new List<string>();

            details.Add(this.Id);
            details.Add(this.Severity.ToString());
            details.Add(this.Description);

            if (this.Exception != null)
            {
                details.Add(this.Exception.ToString());
            }

            return string.Join(" : ", details.ToArray());
        }

        internal static CompositionIssue CreateIssueAsError(string id, Exception ex, string descriptionFormat, params object[] descriptionParameters)
        {
            return new CompositionIssue(id, CompositionIssueSeverity.Error, string.Format(CultureInfo.CurrentCulture, descriptionFormat, descriptionParameters), ex);
        }

        internal static CompositionIssue CreateIssueAsError(string id, string descriptionFormat, params object[] descriptionParameters)
        {
            return new CompositionIssue(id, CompositionIssueSeverity.Error, string.Format(CultureInfo.CurrentCulture, descriptionFormat, descriptionParameters), null);
        }

        internal static CompositionIssue CreateIssueAsWarning(string id, string descriptionFormat, params object[] descriptionParameters)
        {
            return new CompositionIssue(id, CompositionIssueSeverity.Warning, string.Format(CultureInfo.CurrentCulture, descriptionFormat, descriptionParameters), null);
        }
    }
}
